package com.zf.generator.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

import org.apache.commons.io.FileUtils;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.ISelectionService;
import org.eclipse.ui.internal.Workbench;

import com.zf.generator.bean.JavaTemplateArgs;

@SuppressWarnings("unused")
public class SysFileUtil {
	public static String getFileRealPath(String classFullName) {
		StringBuffer sb = new StringBuffer();
		String classFullName1 = classFullName.substring(0, classFullName
				.lastIndexOf("."));
		classFullName1 = classFullName1.replace('.', '\\');
		sb.append(System.getProperty("user.dir")).append("\\src\\").append(
				classFullName1).append(".java");
		return sb.toString();
	}

	public static String getPackageRealPath(String packageFullPath) {
		StringBuffer sb = new StringBuffer();

		packageFullPath = packageFullPath.replace('.', '\\');
		sb.append(System.getProperty("user.dir")).append("\\src\\").append(
				packageFullPath).append("\\");
		return sb.toString();
	}

	public static String convertFileToString(File file) throws Exception {
		BufferedReader br = new BufferedReader(new FileReader(file));
		String line = "";
		StringBuffer buffer = new StringBuffer();
		while ((line = br.readLine()) != null) {
			buffer.append(line);
		}
		String fileContent = buffer.toString();
		return fileContent;
	}

	// public static IProject getCurrentProject(){
	// ISelectionService selectionService =
	// Workbench.getInstance().getActiveWorkbenchWindow().getSelectionService();
	//  
	// ISelection selection = selectionService.getSelection();
	//  
	// IProject project = null;
	// if(selection instanceof IStructuredSelection) {
	// Object element = ((IStructuredSelection)selection).getFirstElement();
	//  
	// if (element instanceof IResource) {
	// project= ((IResource)element).getProject();
	// // } else if (element instanceof PackageFragmentRootContainer) {
	// // IJavaProject jProject =
	// // ((PackageFragmentRootContainer)element).getJavaProject();
	// // project = jProject.getProject();
	// } else if (element instanceof IJavaElement) {
	// IJavaProject jProject= ((IJavaElement)element).getJavaProject();
	// project = jProject.getProject();
	// }
	// }
	// return project;
	// }
	//	
	// public static IProject getProject(){
	// IProject project = null;
	//        
	// //1.根据当前编辑器获取工程
	// IEditorPart part = getActiveEditor();
	// if(part != null){
	// Object object = part.getEditorInput().getAdapter(IFile.class);
	// if(object != null){
	// project = ((IFile)object).getProject();
	// }
	// }
	//        
	// if(project == null){
	// ISelectionService selectionService =
	// Workbench.getInstance().getActiveWorkbenchWindow().getSelectionService();
	// ISelection selection = selectionService.getSelection();
	// if(selection instanceof IStructuredSelection) {
	// Object element = ((IStructuredSelection)selection).getFirstElement();
	//      
	// if (element instanceof IResource) {
	// project= ((IResource)element).getProject();
	// } else if (element instanceof PackageFragmentRootContainer) {
	// IJavaProject jProject =
	// ((PackageFragmentRootContainer)element).getJavaProject();
	// project = jProject.getProject();
	// } else if (element instanceof IJavaElement) {
	// IJavaProject jProject= ((IJavaElement)element).getJavaProject();
	// project = jProject.getProject();
	// } else if(element instanceof EditPart){
	// IFile file = (IFile)
	// ((DefaultEditDomain)((EditPart)element).getViewer().getEditDomain()).getEditorPart().getEditorInput().getAdapter(IFile.class);
	// project = file.getProject();
	// }
	// }
	// }
	//        
	// return project;
	// }

	public static void createPackage(String packageFullPath) {
		File file = new File(getPackageRealPath(packageFullPath));
		if (file.exists())
		{
			file.delete();
			
		}
		file.mkdirs();

	}

//	public static void writeFile(File file,
//			String content) throws IOException {
//		FileWriter fw = new FileWriter(file);
//		PrintWriter out = new PrintWriter(fw);
//
//		out.write(content);
//		out.flush();
//
//		fw.close();
//		out.close();
//	}
	
	public static void createJavaFile(JavaTemplateArgs javaTemplateArgs, String javaCode) throws Exception{
		String fileName=javaTemplateArgs.getClassName();
		fileName=fileName.replace(".", "\\");
		String fileFullName=javaTemplateArgs.getPackageName()+"\\"+fileName+".java";
		
		File file=new File(getFileRealPath(fileFullName));
		if(!file.exists()){
			file.createNewFile();
		}else{
			file.delete();
			file.createNewFile();
		}
		FileUtils.writeStringToFile(file, javaCode, "utf-8");
	}
}
